<?php
/**
 * Created by PhpStorm.
 * User: sunburst
 * Date: 2019-07-18
 * Time: 11:23
 */

namespace api\modules\v1_clinic\controllers;

use Yii;
use common\models\clinic\Project;
use services\api\ClinicTreatmentRoomService;
use yii\web\HttpException;
use yii\web\UploadedFile;


class ClinicTreatmentRoomController extends BaseController
{

    public $modelClass = '';

    /**
     * @api {post} /clinic-treatment-room/index 获取治疗室列表
     * @apiGroup 分组名称
     * @apiName 名称
     * @apiVersion 1.0.2
     *
     * @apiDescription 接口描述
     *
     * @apiParam {Number} project_id  项目ID
     *
     * @apiSuccess {Number} code    状态码，200：请求成功
     * @apiSuccess {String} message   提示信息
     * @apiSuccess {Object} data    返回数据
     *
     */
    public function actionIndexTreatmentRoom()
    {
        $projectId = Yii::$app->request->post('project_id');

        $ret = ClinicTreatmentRoomService::getTreatmentRoomList($projectId);
        return $this->asJson($ret);
    }


    /**
     * @api {post} /clinic-treatment-room/treat-project-list 获取治疗室治疗项目
     * @apiGroup 分组名称
     * @apiName 名称
     * @apiVersion 1.0.2
     *
     * @apiDescription 接口描述
     *
     * @apiParam {Number} treatment_room_id  治疗室ID
     * @apiParam {Number} project_id  项目ID
     *
     * @apiSuccess {Number} code    状态码，200：请求成功
     * @apiSuccess {String} message   提示信息
     * @apiSuccess {Object} data    返回数据
     *
     */
    public function actionGetTreatmentRoomProject()
    {
        $treatmentRoomId = Yii::$app->request->post('treatment_room_id');
        $projectId = Yii::$app->request->post('project_id');
        $ret = ClinicTreatmentRoomService::getTreatmentRoomProject($treatmentRoomId, $projectId);
        return $this->asJson($ret);
    }


    /**
     * @api {post | get} /clinic-treatment-room/get-list 搜索治疗列表
     * @apiGroup 分组名称
     * @apiName 名称
     * @apiVersion 1.0.2
     *
     * @apiDescription 接口描述
     *
     * @apiParam {String} keyword  搜索参数
     * @apiParam {Number} clinic_project_id  治疗项目ID
     * @apiParam {Number} project_id  项目ID
     *
     * @apiSuccess {Number} code    状态码，200：请求成功
     * @apiSuccess {String} message   提示信息
     * @apiSuccess {Object} data    返回数据
     *
     */
    public function actionGetList()
    {
        $keyword = Yii::$app->request->post('keyword');
        $clinicProjectId = Yii::$app->request->post('clinic_project_id');
        $projectId = Yii::$app->request->post('project_id');
        if (!$keyword) {
            return [];
        }
        $ret = ClinicTreatmentRoomService::searchPatientByKeyword($keyword, $clinicProjectId, $projectId);
        return $this->asJson($ret);
    }

    /**
     * @api {post} /clinic-treatment-room/get-patient-last-treat 患者最后一次治疗信息
     * @apiGroup 分组名称
     * @apiName 名称
     * @apiVersion 1.0.2
     *
     * @apiDescription 患者最后一次治疗信息
     *
     * @apiParam {Number} clinic_project_id  治疗项目ID
     * @apiParam {Number} patient_user_id  患者ID
     *
     * @apiSuccess {Number} code    状态码，200：请求成功
     * @apiSuccess {String} message   提示信息
     * @apiSuccess {Object} data    返回数据
     *
     */
    public function actionGetPatientLastTreat()
    {
        $patientId = Yii::$app->request->post('patient_user_id');
        $clinicProjectId = Yii::$app->request->post('clinic_project_id');
        if (!$patientId) {
            throw new HttpException('422', '参数错误');
        }
        $ret = ClinicTreatmentRoomService::getPatientLastTreat($patientId, $clinicProjectId);
        return $this->asJson($ret);
    }

    /**
     * @api {post} /clinic-treatment-room/have-treat-patient-list 已就诊列表
     * @apiGroup 分组名称
     * @apiName 名称
     * @apiVersion 1.0.2
     *
     * @apiDescription 已就诊列表
     *
     * @apiParam {Number} treatment_room_id  治疗室ID
     * @apiParam {Number} project_id  项目ID
     * @apiParam {Number} clinic_project_id  治疗项目ID
     *
     * @apiSuccess {Number} code    状态码，200：请求成功
     * @apiSuccess {String} message   提示信息
     * @apiSuccess {Object} data    返回数据
     *
     */
    public function actionHaveTreatPatientList()
    {
        $clinicProjectId = Yii::$app->request->post('clinic_project_id');
        $projectId = Yii::$app->request->post('project_id');
        $treatmentRoomId = Yii::$app->request->post('treatment_room_id');
        if (!$clinicProjectId) {
            throw new HttpException('422', '参数错误');
        }
        $res = ClinicTreatmentRoomService::haveTreatPatientList($clinicProjectId, $projectId, $treatmentRoomId);
        return $this->asJson($res);
    }

    /**
     * @api {post} /clinic-treatment-room/have-treat-patient-detail 治疗详情
     * @apiGroup 分组名称
     * @apiName 名称
     * @apiVersion 1.0.2
     *
     * @apiDescription 治疗详情
     *
     * @apiParam {Number} clinic_treatment_id  用户治疗信息表ID
     *
     * @apiSuccess {Number} code    状态码，200：请求成功
     * @apiSuccess {String} message   提示信息
     * @apiSuccess {Object} data    返回数据
     *
     */
    public function actionHaveTreatPatientDetail()
    {
        $clinicTreatmentId = Yii::$app->request->post('clinic_treatment_id');
        if (!$clinicTreatmentId) {
            throw new HttpException('422', '参数错误');
        }
        $res = ClinicTreatmentRoomService::haveTreatPatientDetail($clinicTreatmentId);
        return $res;
    }

    /**
     * @api {post} /clinic-treatment-room/start-treat 开始治疗
     * @apiGroup 分组名称
     * @apiName 名称
     * @apiVersion 1.0.2
     *
     * @apiDescription 开始治疗
     *
     * @apiParam {String} $param1  参数说明
     *
     * @apiSuccess {Number} code    状态码，0：请求成功
     * @apiSuccess {String} message   提示信息
     * @apiSuccess {Object} data    返回数据
     *
     */
    public function actionStartTreat()
    {
        $params = Yii::$app->request->post();

        //管理员信息
        $authHeader =  $this->getAuthHeaderInfo();
        $params['operate_id'] = $authHeader->id;
        $params['operate_name'] = $authHeader->realname;

        if (!isset($params['patient_signature']) || !$params['patient_signature']) {
            throw new HttpException('422', '患者签名不能为空');
        }

        if ($params['template'] != Project::TEMPLATE_HALF_311) {
            if ($params['recently_clinic_num'] < 1) {
                throw new HttpException('422', '执行数量不能小于1');
            }
        }

        if ($params['template'] == Project::TEMPLATE_311 || $params['template'] == Project::TEMPLATE_HALF_311) {
            if ($params['dose'] < 1) {
                throw new HttpException('422', '剂量不能小于1');
            }
        }

        $res = ClinicTreatmentRoomService::startTreat($params);
        return $res;
    }

    /**
     * @api {post | get} /clinic-treatment-room/get-treat-detail 治疗记录
     * @apiGroup 分组名称
     * @apiName 名称
     * @apiVersion 1.0.2
     *
     * @apiDescription 治疗记录
     *
     * @apiParam {Number} patient_user_id  参数说明
     * @apiParam {Number} project_id  参数说明
     * @apiParam {Number} treatment_room_id  参数说明
     * @apiParam {Number} clinic_project_id  参数说明
     *
     * @apiSuccess {Number} code    状态码，0：请求成功
     * @apiSuccess {String} message   提示信息
     * @apiSuccess {Object} data    返回数据
     *
     */
    public function actionGetTreatDetail()
    {
        $params = Yii::$app->request->post();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        $res = ClinicTreatmentRoomService::getTreatDetail($params, $page, $pageSize);
        return $res;

    }

    /**
     * 签名上传
     *
     * @return array|string
     */
    public function actionSignUpload()
    {
        if (Yii::$app->request->isPost) {
            $returnPath = '';
            $path = 'uploads/sign/' . date("Ymd");
            if (!file_exists($path)) {
                mkdir($path, 0755, true);
                chmod($path, 0755);
            }

            $tmp = UploadedFile::getInstanceByName('file');
            $ext = $tmp->getExtension();
            if ($tmp) {
                $patch = $path . '/' . date("YmdHis") . '_' . 'x.' . $ext;
                $tmp->saveAs($patch);
                $returnPath .= $patch;
            }

            return $returnPath;
        } else {
            return ['code' => 422, 'message' => '参数错误'];
        }
    }

    /**
     * 大部位列表
     *
     * @return array|\yii\db\ActiveRecord[]
     */
    public function actionGetBodyParts()
    {
        $res = ClinicTreatmentRoomService::getBodyParts();
        return $res;
    }

    /**
     * @api {post | get} /module/get-bc-body-parts 半舱311部位
     * @apiGroup 分组名称
     * @apiName 名称
     * @apiVersion 1.0.2
     *
     * @apiDescription 接口描述
     *
     * @apiParam {String} $param1  参数说明
     *
     * @apiSuccess {Number} code    状态码，0：请求成功
     * @apiSuccess {String} message   提示信息
     * @apiSuccess {Object} data    返回数据
     *
     */
    public function actionGetBcBodyParts()
    {
        $res = ClinicTreatmentRoomService::getBcBodyParts();
        return $res;
    }
}